package top.psjj.mall.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.psjj.mall.comment.Constant;
import top.psjj.mall.comment.Result;
import top.psjj.mall.exception.MallException;
import top.psjj.mall.exception.MallExceptionEnum;
import top.psjj.mall.filter.UserFilter;
import top.psjj.mall.model.dto.AddProductReq;
import top.psjj.mall.model.dto.ProductListReq;
import top.psjj.mall.model.dto.UpdateProductReq;
import top.psjj.mall.model.po.Product;
import top.psjj.mall.model.vo.CartVo;
import top.psjj.mall.service.api.CartService;
import top.psjj.mall.service.api.ProductService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;

/**
 * 商品管理
 */
@RestController
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CartService cartService;
    //商品添加【后台】
    @PostMapping("/admin/product/add")
    public Result add(@RequestBody @Valid AddProductReq addProductReq){
        //将数据赋值到po中
        Product product = new Product();
        BeanUtils.copyProperties(addProductReq,product);
        //插入操作
        productService.save(product);
        return Result.success();
    }

    //商品图片上传【后台】
    @PostMapping("/admin/upload/file")
    public Result upload(HttpServletRequest req,@RequestParam("file") MultipartFile file) throws IOException, URISyntaxException {
        //获得文件名字
        String fileName = file.getOriginalFilename();
        //获得文件后缀名
        String ext = fileName.substring(fileName.lastIndexOf("."));
        //生成文件名字
        String newFileName = UUID.randomUUID().toString()+ext;
        //创建文件
        File fileDirectory = new File(Constant.UPLOAD_DIRECTORY);
        if(!fileDirectory.exists()){
            throw new MallException(MallExceptionEnum.DIR_EXISTS);
        }
        File destFile = new File(Constant.UPLOAD_DIRECTORY,newFileName);
        //上传
        file.transferTo(destFile);
        return Result.success(getHoust(new URI(req.getRequestURL().toString())).toString()+req.getContextPath()+"/images/"+newFileName);

    }
    private URI getHoust(URI uri) throws URISyntaxException {
        URI result = new URI(uri.getScheme(),null,uri.getHost(),uri.getPort(),null,null,null);
        return result;
    }



    //修改产品【后台】
    @PostMapping("/admin/product/update")
    @ApiOperation("修改产品【后台】")
    public Result update(@RequestBody @Valid UpdateProductReq updateProductReq){
        //将数据复制到po中
        Product product = new Product();
        BeanUtils.copyProperties(updateProductReq,product);
        //修改数据
        productService.update(product);
        return Result.success();
    }
    //删除产品【后台】
    @ApiOperation("删除产品【后台】")
    @PostMapping("/admin/product/delete")
    public Result delete(@RequestParam("id") Integer id){
        productService.delete(id);
        return Result.success();
    }
    //商品上下架更改
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public Result batchUpdateSellStatus(@RequestParam("ids") Integer[] ids,
                                        @RequestParam("sellStatus") Integer sellStatus){
        productService.updateBatchSellStatus(ids,sellStatus);
        return Result.success();
    }
    //商品列表
    @GetMapping("/admin/product/list")
    public Result listForAdmin(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize){
        PageInfo pageInfo = productService.listForAdmin(pageNum,pageSize);
        return Result.success(pageInfo);
    }
    //商品详情
    @GetMapping("/product/detail")
    public Result detail(@RequestParam("id") Integer id){
        Product product = productService.detail(id);
        return Result.success(product);
    }
    //商品列表前台
    @GetMapping("/product/list")
    public Result listForUser(ProductListReq productListReq){
        PageInfo pageInfo = productService.listForUser(productListReq);
        return Result.success(pageInfo);
    }
    //购物车添加
    @PostMapping("/cart/add")
    public Result add(@RequestParam("productId") Integer productId,
                      @RequestParam("count") Integer count){
        //用户id后台获取，防止横线越权

        List<CartVo> list = cartService.addCart(UserFilter.currentUser.getId(),productId,count);
        return Result.success(list);
    }
    //购物车列表
    @GetMapping("/cart/list")
    public Result list(){
        List<CartVo> list = cartService.list(UserFilter.currentUser.getId());
        return Result.success(list);
    }
    @PostMapping("/cart/update")
    public Result update(@RequestParam("productId") Integer productId,
                         @RequestParam("count") Integer count){
        List<CartVo> list = cartService.updateCart(UserFilter.currentUser.getId(),productId,count);
        return Result.success(list);
    }

    @PostMapping("/cart/delete")
    public Result cartdelete(@RequestParam("productId") Integer productId){
        List<CartVo> list = cartService.delectCart(UserFilter.currentUser.getId(),productId);
        return Result.success(list);

    }

    @PostMapping("/cart/select")
    public Result select(@RequestParam("productId") Integer productId,
                         @RequestParam("selected") Integer selected){
        List<CartVo> list = cartService.select(UserFilter.currentUser.getId(),productId,selected);
        return Result.success(list);
    }

    @PostMapping("/cart/selectAll")
    public Result selectAll(@RequestParam("selected") Integer selected){
        List<CartVo> list = cartService.selectAll(UserFilter.currentUser.getId(),selected);
        return Result.success(list);

    }
}
